/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * choiceDialog.java
 *
 * Created on 2011-06-08, 00:09:18
 */

package homebudget;

import java.util.LinkedList;
import javax.swing.DefaultComboBoxModel;

/**
 *
 * @author lukasz
 */
public final class choiceDialog extends javax.swing.JDialog {

    /** Creates new form choiceDialog */
    java.awt.Frame parent;    
    public choiceDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        this.parent = parent;
        initComponents();
        
        DataBaseMySql baza = new DataBaseMySql(HomeBudgetApp.getConnectionString());
        DefaultComboBoxModel model = new DefaultComboBoxModel(baza.listaBudzet().toArray());
        jComboBoxBudzet.setModel(model);

        baza.close();
        
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jComboBoxBudzet = new javax.swing.JComboBox();
        jButtonZaladuj = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(homebudget.HomeBudgetApp.class).getContext().getResourceMap(choiceDialog.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jComboBoxBudzet.setName("jComboBoxBudzet"); // NOI18N

        jButtonZaladuj.setText(resourceMap.getString("jButtonZaladuj.text")); // NOI18N
        jButtonZaladuj.setName("jButtonZaladuj"); // NOI18N
        jButtonZaladuj.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonZaladujActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBoxBudzet, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonZaladuj)
                .addContainerGap(65, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBoxBudzet, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonZaladuj)
                    .addComponent(jLabel1))
                .addContainerGap(23, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonZaladujActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonZaladujActionPerformed
        // TODO add your handling code here:
         DataBaseMySql baza = new DataBaseMySql(HomeBudgetApp.getConnectionString());
         
         HomeBudgetApp.setIdbudzetu(jComboBoxBudzet.getSelectedIndex()+1);
         this.parent.setTitle("Wybrano budżet: \""+(String)jComboBoxBudzet.getSelectedItem()+"\"");
         baza.close();
         dispose();
    }//GEN-LAST:event_jButtonZaladujActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                choiceDialog dialog = new choiceDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonZaladuj;
    private javax.swing.JComboBox jComboBoxBudzet;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables

}
